﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Database;
using ShoppingCart;

public partial class ListLinhKien : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showAccessoriesType();
            showAccessories();
           // bindAccessories();
        }

    }

    void showAccessoriesByType(string loai)
    {
            LinhKien lk = new LinhKien();
            DataList1.DataSource = lk.DanhSachLKTheoTenLoaiLK(loai);
            DataList1.DataBind();     
    }



    void showAccessories()
    {

        LinhKien lk = new LinhKien();
        PagedDataSource objPds = new PagedDataSource();
        DataSet ds = lk.DSLinhKien();
        objPds.DataSource = ds.Tables[0].DefaultView;// ds.DefaultViewManager;
        objPds.AllowPaging = true;
        objPds.PageSize = 6;
        objPds.CurrentPageIndex = this.currentPage;
        lblCurrentPage.Text = "Page: " + (this.currentPage + 1).ToString() + " of " + objPds.PageCount.ToString();
        cmdPrev.Enabled = !objPds.IsFirstPage;
        cmdNext.Enabled = !objPds.IsLastPage;
        DataList1.DataSource = objPds;
        //DataList1.DataSource = lk.DSLinhKien();
        DataList1.DataBind();


    }
    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        currentPage -= 1;
        showAccessories();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        currentPage += 1;
        showAccessories();

    }

    public int currentPage
    {
        get
        {
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default to showing the first page
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    void showAccessoriesType()
    {
        LoaiLinhKien lk = new LoaiLinhKien();
        DataSet ds = lk.DSLoaiLinhKien();
        foreach (DataRow masterRow in ds.Tables[0].Rows)
        {
            MenuItem masterItem = new MenuItem((string)masterRow["TenLoaiLK"]);
            loailk.Items.Add(masterItem);
                
        }
            
    }
        
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Mua")
            {
                string ma = e.CommandArgument.ToString();
                LinhKien lk = new LinhKien();
                DataSet product = lk.getLKById(ma);
                Item item = new Item();
                foreach (DataRow row in product.Tables[0].Rows)
                {
                    item.ID = Convert.ToInt32(row["malinhkien"].ToString());
                    item.Name = row["tenlinhkien"].ToString();
                    item.Image = row["hinhanh"].ToString();
                    item.Cost = Convert.ToDouble(row["giaban"].ToString());
                    item.Quantity = 1;
                    item.Total = Convert.ToDouble(row["giaban"].ToString());

                }
                if (Session["ShoppingCart"] == null)
                {
                    Session["ShoppingCart"] = new ShoppingCart.Cart();
                }

                ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
                cart.AddToCart(item);
                Session["ShoppingCart"] = cart;
                string links = Request.ServerVariables["HTTP_REFERER"].ToString();
                Response.Redirect(links);

            }
            else
            {
                string urlDetail = "~/accessoriedetail.aspx?man=" + e.CommandArgument.ToString();
                Response.Redirect(urlDetail);
            }
        }
        protected void loailk_MenuItemClick(object sender, MenuEventArgs e)
        {
            Label4.Text = "You selected " + e.Item.Text + ".";
            showAccessoriesByType(e.Item.Text);
        }
        
}